package com.zoe.wan.android.example.activity.login

import android.app.Application
import android.app.SharedElementCallback
import androidx.databinding.ObservableField
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.zoe.wan.android.example.repositry.Repository
import com.zoe.wan.base.BaseViewModel
import kotlinx.coroutines.launch

class LoginViewModel(application: Application) : BaseViewModel(application) {

    val userNameInput = ObservableField<String>()
    val passwordInput = ObservableField<String>()
    val passwordAgainInput = ObservableField<String>()


    fun login(callback: (userName: String) -> Unit) {
        if (userNameInput.get().isNullOrEmpty() || passwordInput.get().isNullOrEmpty()) {
            ToastUtils.showLong("输入内容不能为空")
            return
        }
        viewModelScope.launch {
           val data = Repository.login(userNameInput.get() ?: "", passwordInput.get() ?: "")
            if (data != null){
                callback.invoke(data.username?: "")
            }
        }
    }

    fun register(callback: (userName: String) -> Unit){
        if (userNameInput.get().isNullOrEmpty() || passwordInput.get().isNullOrEmpty() || passwordAgainInput.get().isNullOrEmpty()) {
            ToastUtils.showLong("输入内容不能为空")
            return
        }
        viewModelScope.launch {
            val data = Repository.register(userNameInput.get() ?: "",passwordInput.get() ?: "", passwordAgainInput.get() ?: "")
            if (data != null){
                callback.invoke(data.username?: "")
            }
        }
    }
}